package com.lyons.generate.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {

	/**
	 * 读取指定文件排除空行
	 * 
	 * @param path
	 * @return
	 */
	public static List<String> readFileToList(String path) {

		List<String> list = new ArrayList<String>();

		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;

		File file = new File(path);

		try {

			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis, "utf-8");
			br = new BufferedReader(isr);

			String line = br.readLine();
			while (null != line) {
				if (line.trim().length() > 0) {
					list.add(line.trim());
				}
				line = br.readLine();
			}
		} catch (FileNotFoundException | UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {

			try {

				if (null != br) {
					br.close();
				}
				if (null != isr) {
					isr.close();
				}
				if (null != fis) {
					fis.close();
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public static String readFileToString(String path, boolean bool) {

		StringBuffer sb = new StringBuffer();

		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader br = null;

		File file = new File(path);

		try {

			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis, "utf-8");
			br = new BufferedReader(isr);

			String line = br.readLine();
			while (null != line) {
				if (bool) {
					if (line.trim().length() > 0) {
						sb.append(line);
					}
					line = br.readLine();
				} else {
					sb.append(line);
					line = br.readLine();
				}
			}
		} catch (FileNotFoundException | UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {

			try {

				if (null != br) {
					br.close();
				}
				if (null != isr) {
					isr.close();
				}
				if (null != fis) {
					fis.close();
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	/**
	 * 保存文件指定的位置
	 *
	 * @param list 需要保存的文件集合
	 * @param path 保存路径, 有验证文件夹是否存在
	 * @param bool 是否追加保存(true:追加保存,false:覆盖保存)
	 */
	public static void saveFile(List<String> list, String path, boolean bool) {

		list.forEach(line -> {
			saveFile(line, path, bool);
		});
	}

	/**
	 * 保存内容到本地文件下
	 * 
	 * @param content 内容
	 * @param path    保存路径
	 * @param bool    保存方式;(true:追加保存,false:覆盖保存)
	 */
	public static void saveFile(String content, String path, boolean bool) {

		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		BufferedWriter bw = null;

		File file = new File(path);

		// 验证文件夹是否存在
		checkFilePathIsExists(file.getParentFile().getAbsolutePath());

		try {

			fos = new FileOutputStream(file, bool);
			osw = new OutputStreamWriter(fos, "utf-8");
			bw = new BufferedWriter(osw);

			bw.write(content);
			bw.newLine();

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != bw) {
					bw.close();
				}
				if (null != osw) {
					osw.close();
				}
				if (null != fos) {
					fos.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 创建目录
	 * 
	 * @param absolutePath
	 */
	private static void checkFilePathIsExists(String absolutePath) {

		File file = new File(absolutePath);
		if (!file.exists()) {
			file.mkdir();
		}
	}

}
